﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Security;
using cognysClient.CognyService;

namespace cognysClient
{
    public partial class Memory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        //method returning a string depending on the users baseline.
        [WebMethod]
        public static string showBaseline(int result)
        {
            try
            {
                //The id for the logged in user.
                Guid person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

                var client = new Service1Client();
                var baseline = client.getBaseline(person);
                var resultState = client.getResultState(person);

                //if no baseLine was registered.
                if (resultState.StateMemory == null)
                {
                    return "Detta resultat kommer registreras som ditt mätvärde.";
                }

                //if baseline was registered one time.
                if (resultState.StateMemory == 1)
                {
                    //if the result is greater then the registered baseline
                    if (baseline.ScoreMemory < result)
                    {
                        return "Detta resultat är ditt slutvärdiga mätvärde, ditt gamla mätvärde var" +
                               baseline.ScoreMemory.ToString();
                    }
                    else
                    {
                        return "Ditt slutvärdiga mätvärde förblir " + baseline.ScoreMemory.ToString();
                    }

                }
                    //if baseline was registered two times.
                else
                {
                    decimal percent = decimal.Parse(result.ToString())/decimal.Parse(baseline.ScoreMemory.ToString())*100;

                    return " Ditt resultat är " + Math.Round(percent, 0).ToString() + "% av ditt mätvärde(" +
                           baseline.ScoreMemory.ToString() + ").";
                    
                }
            }
            catch
            {
                return "Error";
            }
            
        }

        //saves the users result, either to the baseline table, or the memoryResult. Depending on
        //if the user has registered baseline or not.
    [WebMethod]
    public static void SaveResult(int result)
    {
        //The id for the logged in user.
        Guid person = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

        var client = new Service1Client();
        var baseline = client.getBaseline(person);
        var resultState = client.getResultState(person);

        //if the resultstate is null (the user has no registered baseline).
        if (resultState.StateMemory == null)
        {
            //adds to baseline.
            client.editBaseLine(person, "memory", result);

            //sets resultstate to 1.
            client.editResultState(person, "memory", 1);

        }
            //if the resultState is 1 (the user has registered baseline 1 of 2.)
        else if (resultState.StateMemory == 1)
        {
            //if the result is greater then the previous baseline, it is updated.
            if (result > baseline.ScoreMemory)
            {
                client.editBaseLine(person, "memory", result);
            }
            //sets resultstate to 2.
            client.editResultState(person, "memory", 2);
        }

            //when resultstate is 2 (the user has registered baseline 2 times).
            //the result is added to resultmemory.
        else
        {
            ResultMemory memory = new ResultMemory();

            memory.Person = person;
            memory.regDate = DateTime.Now;
            memory.Score = result;

            client.AddMemoryResult(memory);
        }   
    }

    }
}